// pages/register/register.ts
const app = getApp()  // 获取App实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: app.getImgUrl('logo.png'),
    ispass: true,

    "phone": "",
    yespassword: "",
    "password": "",
    "name": "",
    "code": "",

    showyan2: 1,
    showyan1: 1,
    daojishi: 0,
    ischack: false,
    time: null
  },
  qyregister() {
    wx.navigateTo({
      url: "/pages/enterprise_register/enterpriseRegister"
    })
  },
  changeyan1() {
    this.setData({
      showyan1: this.data.showyan1 == 1 ? 2 : 1
    })
  },
  changeyan2() {
    this.setData({
      showyan2: this.data.showyan2 == 1 ? 2 : 1
    })
    console.log(this.data.showyan1)
  },
  changechack() {
    this.setData({
      ischack: !this.data.ischack
    })
  },
  showerror(content) {
    wx.showToast({
      title: content,
      icon: 'error',
      duration: 1500
    })
  },
  //获取验证码按钮
  getyanzheng() {
    if (!this.data.phone) {
      this.showerror('请输入管理账号')
      return
    }
    if (this.data.daojishi > 0) {
      wx.showToast({
        title: '请勿过于频繁获取验证码',
        icon: 'error',
        duration: 1500
      })
      return
    }
    app.axios(
      'user',
      {
        op: "ajaxSendCode",
        "phone": this.data.phone,
      },
      false
    )
      .then(res => {
        this.setData({
          daojishi: 30
        })

        this.setData({
          time: setInterval(() => {

            this.setData({
              daojishi: this.data.daojishi - 1
            })
            if (this.data.daojishi <= 0) {
              this.setData({
                daojishi: 0
              })
              if (this.data.time) {
                clearInterval(this.data.time)
              }
            }
            console.log(this.data.daojishi)
          }, 1000)
        })
        wx.showToast({
          title: '验证码发送成功',
          icon: 'success',
          duration: 1500
        })
      });

  },
  register() {
    if (!this.data.ischack) {
      this.showerror('请阅读并同意用户协议')
      return
    }
    if (!this.data.name) {
      this.showerror('请输入用户名')
      return
    }
    if (!this.data.phone) {
      this.showerror('请输入手机号')
      return
    }
    if (!this.data.code) {
      this.showerror('请输入验证码')
      return
    }
    // 判断两次密码是否相同
    if (this.data.yespassword != this.data.password) {
      this.showerror('两次输入的密码必须相同')
      return
    }
    app.axios(
      'user',
      {
        op: "ajaxUserReg2",
        "phone": this.data.phone,
        "password": this.data.password,
        "name": this.data.name,
        "code": this.data.code,
      },
      false
    )
      .then(res => {
        if (res.code) {
          this.gopage(res.item[0])
        } else {
          wx.showToast({
            title: '注册失败',
            duration: 1500
          })
        }
      });
  },
  gopage(items) {
    // 创建session对象
    const sessionData = {
      mkey: items.mkey,
      userid: items.userid,
      corpid: items.corpid,
      corpName: items.corpName,
      zzstatus: items.status
    };

    // 双重存储：内存 + 本地存储
    app.globalData.session = sessionData;
    wx.setStorageSync('session', sessionData);
    wx.showToast({
      title: '注册成功,即将跳转',
      icon: 'success',
      duration: 1500
    })
    setTimeout(() => {
      app.navigateTo('/pages/index/index', 'switchTab')
    }, 2000);
  },
})